// pages/user-doctor-details/user-doctor-details.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl, // 本地图片
    goodssort: '', // 医美商品类别
    casesort: '', // 案例类别
    diarysort: '', // 日记类别
    info: '',
    doctorid: '',
    onetypeservicelist: [], // 一级服务类产品列表
    goodsinfo: [], // 商品
    caseinfo: [], // 案例
    live: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   
    this.ctx = wx.createLivePlayerContext('player')
    var doctorid = options.id;
    this.setData({
      'doctorid': doctorid,
    });

    this.loadDoctorDetail(); // 加载个人信息
    this.loadDoctorGoodsInfo(); // 加载商品信息
    this.loadDoctorCaseInfo(); // 加载案例信息
    // this.loadOneTypeServiceList(); // 获取一级分类
    this.getDoctorLive();

  },

  // 一级服务产品类别列表
  loadOneTypeServiceList: function(e) {
    var that = this;
    app.request({
      url: 'shopp/goodsTypeApi/getPage',
      data: {
        'type': 2,
      },
      success: function(res) {
        that.setData({
          'onetypeservicelist': res.data,
          'diarysort': res.data[0].id,
        })
      }
    })
  },

  // 加载医生个人信息
  loadDoctorDetail: function(e) {
    var that = this;
    app.request({
      url: 'doctor/doctor/getDoctorHomeDetail',
      data: {
        'doctorId': that.data.doctorid,
      },
      success(res) {
        that.setData({
          info: res.data
        })
      }
    })
  },

  // 加载医生商品信息
  loadDoctorGoodsInfo: function(e) {
    var that = this;
    app.request({
      url: 'doctor/doctor/getDoctorGoods',
      data: {
        'doctorId': that.data.doctorid,
        'typeId': that.data.goodssort,
      },
      success(res) {
        if (res.data.type.length) {
          if (that.data.goodssort) {
            that.setData({
              goodsinfo: res.data,
            });
          } else {
            that.setData({
              goodsinfo: res.data,
              goodssort: res.data.type[0].typeId,
            });
          }
        }
      }
    })
  },

  // 加载医生案例信息
  loadDoctorCaseInfo: function(e) {
    var that = this;
    app.request({
      url: 'doctor/doctor/getDoctorExample',
      data: {
        'doctorId': that.data.doctorid,
        'typeId': that.data.casesort,
      },
      success(res) {
        if (res.data.type.length) {
          if (that.data.casesort) {
            that.setData({
              caseinfo: res.data,
            });
          } else {
            that.setData({
              caseinfo: res.data,
              casesort: res.data.type[0].typeId,
            });
          }
        }
      }
    })
  },

  // 切换医美商品
  bindGoodsSort: function(e) {
    var goodssort = e.currentTarget.dataset.goodssort;
    if (goodssort != this.data.goodssort) {
      this.setData({
        'goodssort': goodssort,
      });
      this.loadDoctorGoodsInfo(); // 加载商品信息
    } else {
      return false;
    }
  },

  // 切换案例类型
  bindCaseSort: function(e) {
    var casesort = e.currentTarget.dataset.casesort;
    if (casesort != this.data.casesort) {
      this.setData({
        'casesort': casesort,
      });
      this.loadDoctorCaseInfo(); // 加载案例信息
    } else {
      return false;
    }
  },

  // 切换日记类型
  bindDiarySort: function(e) {
    var diarysort = e.currentTarget.dataset.diarysort;
    if (diarysort != this.data.diarysort) {
      this.setData({
        'diarysort': diarysort,
      });
    } else {
      return false;
    }
  },

  // 关注按钮
  bindFollow: function(e) {
    var doctorid = e.currentTarget.dataset.doctorid;
    var that = this;
    app.request({
      url: 'doctor/member/attention',
      data: {
        userId: doctorid,
      },
      success(res) {
        that.loadDoctorDetail();
        app.request({
          url: 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 5,
          },
         
          success(res) {
            console.log('给美丽币给美丽值', res);
          }
        })
      }
    })
  },


  // 跳转往期视频
  agoLive() {
    wx.navigateTo({
      url: "../user-doctor-video/user-doctor-video?doctorid=" + this.data.doctorid
    });
  },

  // 关注
  onShareAppMessage: function() {
    return {
      title: '分享好友获取美丽币！',
      path: '/pages/index/index',
      success: function() {
        app.request({
          url: 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 3
          },
          success(res) {
            console.log('分享给美丽币给美丽值', res);
          }
        })
      }
    }
  }
})